<?php
	class Admbooks extends CI_Controller{
		//主页显示图书信息
		public function admbookspage_list(){
			//加载时区
			date_default_timezone_set('Asia/Shanghai');
				//加载模型
			$this->load->model("Admbooksmodel","usermodel");
			$perpage=5;
			if (intval($this->uri->segment(3)) > 0) {
				$prevpage = intval($this->uri->segment(3)) - 1;
			} else {
				$prevpage = intval($this->uri->segment(3));
			}
			$data["data"] = $this->usermodel->page_list($prevpage * $perpage,$perpage);
			// var_dump($result);

			//下面开始分页
			//加载类库
			 $this->load->library('pagination');
	        // $perPage=10;//每页10条
	        //配置项设置
	        $config['base_url']=site_url("Admbooks/admbookspage_list/");
	        $config['total_rows']=$this->db->count_all_results('books');
	        $config['per_page'] = $perpage;

			$config['use_page_numbers'] = TRUE;

			$config['full_tag_open'] = '<ul class="page">';
			$config['full_tag_close'] = '</ul>';

			$config['first_link'] = "<li style='color:white;'>首页</li>";
			$config['last_link'] = "<li style='color:white;'>尾页</li>";
        	$config['prev_link']="上一页";
        	$config['next_link']="下一页";
			
			// $config['prev_link'] = '&lt;&lt;';
			$config['prev_tag_open'] = '<li>';
			$config['prev_tag_close'] = '</li>';

			// $config['next_link'] = '&gt;&gt;';
			$config['next_tag_open'] = '<li>';
			$config['next_tag_close'] = '</li>';

			$config['cur_tag_open'] = "<li class='active'>";
			$config['cur_tag_close'] = '</li>';

			$config['num_tag_open'] = '<li>';
			$config['num_tag_close'] = '</li>';

			$this->pagination->initialize($config);

			$data["links"] = $this->pagination->create_links();
			//加载的视图文件
			$this->load->view("Admbooks/books_list",$data);
		}
		//图书添加
		public function book_add(){
			$this->load->model("Admbooksmodel");
			$this->load->helper(array('form', 'url'));
        	$this->load->library('form_validation');
		    // $data["addBook_ID"] = $this->input->post("addBook_ID");
			$data["ISBN"] = $this->input->post("addISBN");
			$data["Type"] = $this->input->post("addType");
		    $data["Title"] = $this->input->post("addTitle");
		    $data["Author"] = $this->input->post("addAuthor");
			$data["Publisher"] = $this->input->post("addPublisher");
		    $data["Number"] = $this->input->post("addNumber");
		    $data["Stocks"] = $this->input->post("addStocks");
		    //判断用户名不能相同
		    if(!$book_row = $this->Admbooksmodel->book_row($data["ISBN"])){
		        if ($res = $this->Admbooksmodel->book_add($data)) {
				    # 注册成功
					echo "添加成功,请关闭窗口";			         		
				    }else{
				        echo "error";
				    }
		        }else{
		            echo "图书编号已存在";
		    }	            
		}
		//增加模态框
		public function add(){			
			$this->load->view('Admbooks/books_add');
		}
		//编辑模态框
		public function edit(){
			$this->load->view('Admbooks/books_edit');
		}
		//图书编辑查询返回数据到编辑页
		public function book_edit(){
			$this->load->database(); 
			$this->load->model("Admbooksmodel");
			$id = $_GET['id'];
			$data['data'] = $this->Admbooksmodel->book_id($id);
			$this->load->view('Admbooks/books_edit',$data);
		}
		//图书编辑修改
		public function book_save(){
			$this->load->database();
			$this->load->model("Admbooksmodel");
			$data["editBook_ID"] = $this->input->post("editBook_ID");
			$data["editISBN"] = $this->input->post("editISBN");
			$data["editType"] = $this->input->post("editType");
		    $data["editTitle"] = $this->input->post("editTitle");
		    $data["editAuthor"] = $this->input->post("editAuthor");
			$data["editPublisher"] = $this->input->post("editPublisher");
		    $data["editNumber"] = $this->input->post("editNumber");
		    $data["editStocks"] = $this->input->post("editStocks");
		    
		    $datas = array("ISBN"=>$data["editISBN"],"Type"=>$data["editType"],"Title"=>$data["editTitle"],"Author"=>$data["editAuthor"],"Publisher"=>$data["editPublisher"],"Number"=>$data["editNumber"],"Stocks"=>$data["editStocks"]);
			$bool=$this->db->update('books',$datas,array('Book_ID'=>$data["editBook_ID"]));
			if($bool){
				echo "修改成功,请关闭窗口";
			}else{
				echo "修改失败";
			}
		}
		//图书删除
		public function book_dele(){
			// $id = (int)input('post.id');
			$id = $_GET['id'];
			$this->load->database();
			$this->load->model("Admbooksmodel");
			$res = $this->Admbooksmodel->book_dele($id);
			$this->admbookspage_list();
		}
		//模糊查询
		public function book_query(){
			$this->load->database();
			date_default_timezone_set('Asia/Shanghai');
			$this->load->model("Admbooksmodel");
			$queryname = $_POST['queryname'];
			$data["data"] = $this->Admbooksmodel->book_vague($queryname);
			if($data["data"]){
				$this->load->view('Admbooks/books_list',$data);	
			}else{
				echo "<scirpt>请输入查询关键字</script>";
			}
		}

	}
?>